<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";

	//允许填充字段
	protected $allowFill = ['*' ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]

	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];


	/**
	 * 文章添加
	 */
	public  function articleadd(){
		//请文章表中添加数据
         $this->validate=[
			 ['title','isnull','标题不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['author','isnull','作者不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['digest','isnull','摘要不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['keywords','isnull','关键字不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['decs','isnull','描述不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['content','isnull','内容不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['thumb','isnull','缩略图不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
			 ['cid','isnull','请选择类别',self::MUST_VALIDATE ,self::MODEL_INSERT]
		 ];
		if(!isset($_POST['tags'])){
			return ['valid'=>0,'message'=>'前选择标签名'];
		}
//		p($_POST);die;
        $this->title=$_POST['title'];
        $this->thumb=$_POST['thumb'];
        $this->digest=$_POST['digest'];
        $this->keywords=$_POST['keywords'];
        $this->decs=$_POST['decs'];
        $this->content=$_POST['content'];
        $this->cid=$_POST['cid'];
		$this->author=$_POST['author'];
		$this->pubtime=time();
		$res1=$this->save();

//		向文章数据表中添加数据
		$Model = new Articledata();
		$Model['title'] = $_POST['title'];
		$Model['author'] = $_POST['author'];
		$Model['digest'] = $_POST['digest'];
		$Model['keywords'] = $_POST['keywords'];
		$Model['content'] = $_POST['content'];
		$Model['decs'] = $_POST['decs'];
		$Model['pubtime']=$this->pubtime;
		$res2=$Model->save();
		//向标签中间表中插入数据
		$tags=$_POST['tags'];
//		p($tags);die;
		$Model1 = new Article_tag();
		foreach($tags as $k=>$v){
//            $Model1['aid']=$res1;
//            $Model1['tid']=$v;
			$res3=Db::table('article_tag')->insert(['aid'=>$res1,'tid'=>$v]);
//			$res3=$Model1->save();
		}
		//判断三张表是否都插入成功
		$res=$res1 && $res2 &&$res3;
		if($res){
			return ['valid'=>$res,'message'=>'添加成功'];
		}
		else{
			return ['valid'=>$res,'message'=>'添加失败'];
		}
	}
	/**
	 * 获取所有的数据
	 */
	public function getarticles(){
//	  return Db::table('article')->get();
	 return	 Db::table('article')
			->join('category','article.cid','=','category.cid')
			->get();
	}

	/**
	 * @param $aid   传入指定文章的aid
	 * @return mixed
	 */
    public function getarticle($aid){
		return Db::table('article')->where('aid',$aid)->first();
	}

	/**
	 * @param $aid
	 * @return array
	 * 文章编辑
	 */
	public function articleedit($aid){
//		$this->validate=[
//			['title','isnull','标题不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['author','isnull','作者不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['digest','isnull','摘要不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['keywords','isnull','关键字不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['decs','isnull','描述不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['content','isnull','内容不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['thumb','isnull','缩略图不能为空',self::MUST_VALIDATE ,self::MODEL_INSERT],
//			['cid','isnull','请选择类别',self::MUST_VALIDATE ,self::MODEL_INSERT]
//		];
		if(!isset($_POST['tags'])){
			return ['valid'=>0,'message'=>'前选择标签名'];
		}

		$this->title=$_POST['title'];
		$this->thumb=$_POST['thumb'];
		$this->digest=$_POST['digest'];
		$this->keywords=$_POST['keywords'];
		$this->decs=$_POST['decs'];
		$this->content=$_POST['content'];
		$this->cid=$_POST['cid'];
		$this->author=$_POST['author'];
		$this->updatetime=time();
		$res1=Db::table('article')->where("aid",$aid)->update(['title'=>$_POST['title'],
			'thumb'=>$_POST['thumb'],
			'digest'=>$_POST['digest'],
			'keywords'=>$_POST['keywords'],
			'decs'=>$_POST['decs'],
			'content'=>$_POST['content'],
			'cid'=>$_POST['cid'],
			'author'=>$_POST['author'],
			'updatetime'=>$_POST['updatetime']
			]);
		//文章数据表更新
		$articeldata=Articledata::find($aid); // 查找主键为1的数据
		$articeldata['title'] = $_POST['title'];
		$articeldata['author'] = $_POST['author'];
		$articeldata['digest'] = $_POST['digest'];
		$articeldata['keywords'] = $_POST['keywords'];
		$articeldata['content'] = $_POST['content'];
		$articeldata['decs'] = $_POST['decs'];
		$this->updatetime=time();
		$res2=Db::table('articledata')->where("aid",$aid)->update(['title'=>$_POST['title'],
			'author'=>$_POST['author'],
			'digest'=>$_POST['digest'],
			'keywords'=>$_POST['keywords'],
			'decs'=>$_POST['decs'],
			'content'=>$_POST['content'],
			'cid'=>$_POST['cid'],
			'author'=>$_POST['author'],
			'updatetime'=>$_POST['updatetime']
		]);
		//标签中中间表
		$tags=$_POST['tags'];
//		p($tags);die;
		$Model1 = new Article_tag();
		foreach($tags as $k=>$v){
			Db::table('article_tag')->where('aid',$aid)->delete();
			$res3=Db::table('article_tag')->insert(['aid'=>$aid,'tid'=>$v]);
		}
		//判断三张表是否都插入成功
//		p($res1);
//		p($res2);
//		p($res3);die;
		$res=$res1 && $res2 &&$res3;
		if($res){
			return ['valid'=>$res,'message'=>'修改成功'];
		}
		else{
			return ['valid'=>$res,'message'=>'修改失败'];
		}
	}

	/**
	 * @param $num
	 * @return array
	 * 删除到回收站
	 */
 	public function delToArticle($aid){
//            p($aid);
		$res=$this->where('aid', '=', $aid)->update(['recycle' => 1]);
		$res1=Db::table('articledata')->where('aid', '=', $aid)->update(['recycle' => 1]);
		if($res && $res1){
			return ['valid'=>1,'message'=>'删除回收站成功'];
		}else{
			return ['valid'=>0,'message'=>'删除回收站失败'];
		}
	}

	/**
	 * @param $num
	 * @param $recycle
	 * @return
	 * 从回收站恢复
	 */
	public  function backTo($aid){
		$res=$this->where('aid', '=', $aid)->update(['recycle' => 0]);
		$res1=Db::table('articledata')->where('aid', '=', $aid)->update(['recycle' => 0]);
		if($res && $res1){
			return ['valid'=>1,'message'=>'恢复成功'];
		}else{
			return ['valid'=>0,'message'=>'恢复失败'];
		}
	}


	public function getAllData($num,$recycle)
	{
		//统计文章表不在回收站的数据总数
		$count = Db::table('article')->where('recycle',$recycle)->count();
		$page = Page::row($num)->make($count);
		Page::desc(['pre'=>'上楼', 'next'=>'下楼','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
		$data = Db::table('article')
			->join('category','article.cid','=','category.cid')
			->where('recycle',$recycle)->limit(Page::limit())
			->get();
		return ['page'=>$page,'data'=>$data];
	}

}
